package com.sino.androiddesign.anim

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.*
import com.sino.androiddesign.R
import kotlinx.android.synthetic.main.activity_tween.*

/**
 * 补间动画
 * 参考地址: https://www.jianshu.com/p/733532041f46
 *
 * 1: 作用对象 : android 视图控件(view)
 *
 * 2: 原理
 *  通过确定开始的视图样式 & 结束的视图样式,中间动画变化过程由系统补全来确定一个动画
 *  2.1: 结束的视图样式: 平移,缩放,旋转和透明度样式
 *  2.2: 即补间动画的动画效果就是:平移,缩放,旋转和透明度动画
 * 3: 分类
 *  根据动画效果,补间动画可以分为四种动画:
 *  平移动画,缩放动画,旋转动画,透明度动画
 *
 *      名称                  原理              对应Animation的子类
 *  平移动画(Translate)     移动视图的位置        TranslateAnimation类
 *  缩放动画(Scale)         放大/缩小 视图的大小   ScaleAnimation类
 *  旋转动画(Rotate)        旋转视图的角度         RotateAnimation类
 *  透明度动画(Alpha)        改变视图的透明度       AlphaAnimation类
 *
 * 4:具体使用:
 * 补间动画的使用方式分为两种:xml代码 /java代码
 *
 * 前者动画描述可读性更好,后者动画可以动态创建
 *
 * 5:补间动画 只是视图上发生变化,但是其真正的属性并没有发生变化(比如原来的坐标)
 */
class TweenActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tween)

        initTranslateAnimation()
        initScaleAnimation()
        initRotateAnimation()
        initAlphaAnimation()
        initSetAnimation()
//        initViewGroupAnimation()
    }

    /**
     * 4.1: 平移动画
     *  4.1.1: xml ==>
     *    a: 在res/anim 的文件夹里创建动画效果 .xml文件
     *    b:在java代码中创建Animation对象并播放
     */
    private fun initTranslateAnimation() {
        tween_translate_xml_btn.setOnClickListener {
            var transAnimation = AnimationUtils.loadAnimation(this, R.anim.tween_translate_animation)
            tween_translate_iv.startAnimation(transAnimation)
        }

        tween_translate_java_btn.setOnClickListener {
            var translateAnimation = TranslateAnimation(0f, 800f, 0f, 100f)
            translateAnimation.duration = 2000
            translateAnimation.fillBefore = true
            translateAnimation.repeatCount = 2
            translateAnimation.repeatMode = Animation.REVERSE
            tween_translate_iv.startAnimation(translateAnimation)
        }
    }

    /**
     * 4.2  缩放动画
     *
     *
     */
    private fun initScaleAnimation() {

        tween_scale_xml_btn.setOnClickListener {
            var animation = AnimationUtils.loadAnimation(this, R.anim.tween_scale_animation)
            tween_scale_iv.startAnimation(animation)
            println("123")
        }

        tween_scale_java_btn.setOnClickListener {
            println("123")
//            var scaleAnimation = ScaleAnimation(2.0f, 0.5f, 0.5f, 2.0f)
            var scaleAnimation = ScaleAnimation(2.0f, 0.5f, 0.5f, 2.0f, Animation.ABSOLUTE, 0.3f, Animation.ABSOLUTE, 0.8f)
            scaleAnimation.duration = 4000
            scaleAnimation.repeatCount = 4
            scaleAnimation.repeatMode = Animation.RESTART
            scaleAnimation.fillAfter = true
            scaleAnimation.startOffset = 2000
            tween_scale_iv.startAnimation(scaleAnimation)
        }

    }

    /**
     * 4.3 旋转动画
     */
    private fun initRotateAnimation() {
        tween_rotate_xml_btn.setOnClickListener {
            var loadAnimation = AnimationUtils.loadAnimation(this, R.anim.tween_rotate_animation)
            tween_rotate_iv.startAnimation(loadAnimation)
        }

        tween_rotate_java_btn.setOnClickListener {

            var rotateAnimation = RotateAnimation(180f, -90f, Animation.ABSOLUTE, 0.3f, Animation.ABSOLUTE, 0.3f)
            rotateAnimation.duration = 2000
            rotateAnimation.fillAfter = true
            rotateAnimation.repeatCount = 2
            rotateAnimation.repeatMode = Animation.REVERSE
            tween_rotate_iv.startAnimation(rotateAnimation)

        }
    }

    /**
     * 4.4 透明动画
     */
    private fun initAlphaAnimation() {
        tween_alpha_xml_btn.setOnClickListener {
            var loadAnimation = AnimationUtils.loadAnimation(this, R.anim.tween_alpha_animation)

            tween_alpha_iv.startAnimation(loadAnimation)
        }

        tween_alpha_java_btn.setOnClickListener {

            var alphaAnimation = AlphaAnimation(0.5f, 1.0f)
            alphaAnimation.duration = 2000
            alphaAnimation.startOffset = 100
            alphaAnimation.repeatCount = 4
            alphaAnimation.repeatMode = Animation.REVERSE
            alphaAnimation.fillAfter = true
            tween_alpha_iv.startAnimation(alphaAnimation)

        }
    }

    /**
     * 4.5 组合动画
     */
    private fun initSetAnimation() {
        tween_set_xml_btn.setOnClickListener {
            var loadAnimation = AnimationUtils.loadAnimation(this, R.anim.tween_set_animation)
            tween_set_iv.startAnimation(loadAnimation)
        }

        tween_set_java_btn.setOnClickListener {


            var animationSet = AnimationSet(true)

            animationSet.duration = 2000
            animationSet.repeatCount = 2
            animationSet.repeatMode = Animation.REVERSE

            var translateAnimation = TranslateAnimation(0f, 200f, 0f, 0f)
            translateAnimation.duration = 2000
            translateAnimation.repeatMode = Animation.REVERSE
            translateAnimation.repeatCount = 2

            var scaleAnimation = ScaleAnimation(1.5f, 0.5f, 1f, 1f)
            translateAnimation.duration = 2000
            scaleAnimation.repeatMode = Animation.REVERSE
            scaleAnimation.repeatCount = 2

            var rotateAnimation = RotateAnimation(270f, 0f)
            rotateAnimation.duration = 2000
            rotateAnimation.repeatMode = Animation.REVERSE
            rotateAnimation.repeatCount = 2

            var alphaAnimation = AlphaAnimation(0.0f, 1.0f)
            alphaAnimation.duration = 2000
            alphaAnimation.repeatMode = Animation.REVERSE
            alphaAnimation.repeatCount = 2

            animationSet.addAnimation(translateAnimation)
            animationSet.addAnimation(scaleAnimation)
            animationSet.addAnimation(rotateAnimation)
            animationSet.addAnimation(alphaAnimation)

            tween_set_iv.startAnimation(animationSet)

        }
    }


    /**
     * 4.6 对于viewGroup的子view 添加动画效果(参考地址: https://www.jianshu.com/p/5d811aaf4541)
     * xml ==>
     * 1:创建子view的动画效果  anim/tween_view_animation.xml
     * 2:创建viewGroup的动画文件  anim/tween_viewgroup_animation
     * 3: 在viewGroup中使用 android:layoutAnimation="anim/tween_viewgroup_animation"
     *
     * java 代码创建如下 :
     */
    private fun initViewGroupAnimation() {
        var animation = AnimationUtils.loadAnimation(this, R.anim.tween_view_animation)

        var controller = LayoutAnimationController(animation)
        controller.delay=0.5f
        controller.order = LayoutAnimationController.ORDER_NORMAL
        tween_ll.layoutAnimation  =controller


    }
}
